/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ESpell.h"

#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QFile>

#include <QtXml/QDomDocument>

#include "Parser.h"

using namespace Bio;

const QString	c_espellUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/espell.fcgi";

namespace Bio {

class ESpellParser : public Parser {
public:
	ESpellParser( ESpell *spell );
	~ESpellParser();

	virtual bool	parse( QIODevice &dev );

private:

	bool	parseSpellResult( const QDomElement &root );

	ESpell	*m_spell;
};

}; // Bio

///////////////////////////////////////////////////////////////////////////////
//	ESpell
///////////////////////////////////////////////////////////////////////////////

/**
	@class ESpell
	@brief Implementation of the Entrez 'ESpell' function.

	Provides an interface to the NCBI Entrez 'ESpell' function.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez ESpell utility.
**/

/**
	Default Constructor. Creates a new ESpell operation.
**/
ESpell::ESpell()
{
}

/**
**/
ESpell::ESpell( const QString &db, const QString &term ) : m_db( db ), m_term( term )
{
}

/**
**/
ESpell::~ESpell()
{
}

/**
**/
bool ESpell::exec()
{
	QUrl			url( c_espellUrl );
	QUrlQuery		urlQuery;
	ESpellParser	parser( this );

	m_corrections.clear();

	if( m_db.isEmpty() ){
		error( "No database specified for ESpell." );
		return false;
	}

	urlQuery.addQueryItem( "db", m_db );

	if( m_term.isEmpty() ){
		error( "No search term specified for ESpell." );
		return false;
	}

	urlQuery.addQueryItem( "term", m_term );

	url.setQuery( urlQuery );

	return execGet( url, parser );
}

/**
**/
int ESpell::getNumCorrections() const
{
	return m_corrections.count();
}

/**
**/
QString ESpell::getCorrection( int index ) const
{
	if( index < 0 || index >= m_corrections.count() )
		return QString::null;

	return m_corrections[index];
}

/**
**/
QStringListIterator ESpell::getCorrectionIterator() const
{
	return QStringListIterator( m_corrections );
}

/**
**/
void ESpell::setDatabase( const QString &db )
{
	m_db = db;
}

/**
**/
QString ESpell::getDatabase() const
{
	return m_db;
}

/**
**/
void ESpell::setTerm( const QString &term )
{
	m_term = term;
}

/**
**/
QString ESpell::getTerm() const
{
	return m_term;
}

/**
**/
void ESpell::addCorrection( const QString &correction )
{
	m_corrections.append( correction );
}

/**
**/
ESpellParser::ESpellParser( ESpell *spell ): m_spell( spell )
{
}

/**
**/
ESpellParser::~ESpellParser()
{
}

/**
**/
bool ESpellParser::parse( QIODevice &dev )
{
	QDomDocument	doc;
	QString			errMsg;
	int				errLine, errCol;

	if( !doc.setContent( &dev, &errMsg, &errLine, &errCol ) ){
		error( QString( "Error parsing ESpell response (%1:%2): %3" ).arg( errLine ).arg( errCol ).arg( errMsg ) );
		return false;
	}

	bool		status = true;
	QDomNode	node = doc.firstChild();

	while( !node.isNull() ){
		QDomElement	elem = node.toElement();

		if( !elem.isNull() ){
			if( elem.tagName() == "eSpellResult" )
				status = parseSpellResult( elem );
		}

		node = node.nextSibling();
	};

	return status;
}

/**
**/
bool ESpellParser::parseSpellResult( const QDomElement &root )
{
	QDomNode	node = root.firstChild();

	while( !node.isNull() ){
		QDomElement elem = node.toElement();

		if( !elem.isNull() ){
			if( elem.tagName() == "CorrectedQuery" )
				m_spell->addCorrection( elem.text() );
		}

		node = node.nextSibling();
	}

	return true;
}
